package com.im.common.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.im.common.enums.exception.BaseExceptionEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 返回结果实体类
 */
@Data
@ApiModel
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class RestResponse<T> {

    public static final String DEFAULT_SUCCESS_MESSAGE = "success";
    public static final String DEFAULT_ERROR_MESSAGE = "error";
    public static final Integer DEFAULT_SUCCESS_CODE = 0;
    public static final Integer DEFAULT_ERROR_CODE = 500;

    @ApiModelProperty(value = "请求是否成功")
    private boolean status;
    @ApiModelProperty(value = "响应状态码")
    private Integer code;
    @ApiModelProperty(value = "提示信息")
    private String message;
    @ApiModelProperty(value = "逻辑数据")
    private T data;

    public RestResponse() {
        this.status = true;
        this.code = DEFAULT_SUCCESS_CODE;
        this.message = DEFAULT_SUCCESS_MESSAGE;
    }

    public RestResponse(boolean status, String message) {
        this.status = status;
        this.message = message;
    }

    public RestResponse(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public RestResponse(boolean status, Integer code, String message) {
        this.status = status;
        this.code = code;
        this.message = message;
    }

    public RestResponse(boolean status, Integer code, String message, T data) {
        this.status = status;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> RestResponse<T> success(T object) {
        return new RestResponse(true, DEFAULT_SUCCESS_CODE, DEFAULT_SUCCESS_MESSAGE, object);
    }

    public static <T> RestResponse<T> success(String message) {
        return new RestResponse(true, DEFAULT_SUCCESS_CODE, message);
    }

    public static <T> RestResponse<T> success(String message, T object) {
        return new RestResponse(true, DEFAULT_SUCCESS_CODE, message, object);
    }

    public static <T> RestResponse<T> success() {
        return new RestResponse(true, DEFAULT_SUCCESS_CODE, DEFAULT_SUCCESS_MESSAGE);
    }

    public static <T> RestResponse<T> error() {
        return new RestResponse(false, DEFAULT_ERROR_CODE, DEFAULT_ERROR_MESSAGE);
    }

    public static <T> RestResponse<T> error(String message) {
        return new RestResponse(false, DEFAULT_ERROR_CODE, message);
    }

    public static <T> RestResponse<T> error(T object) {
        return new RestResponse(false, DEFAULT_ERROR_CODE, DEFAULT_ERROR_MESSAGE, object);
    }

    public static <T> RestResponse<T> error(BaseExceptionEnum errorCode) {
        return new RestResponse(false, errorCode.getCode(), errorCode.getMsg());
    }

    public static <T> RestResponse<T> error(Integer code, T object) {
        return new RestResponse(false, code, DEFAULT_ERROR_MESSAGE, object);
    }

    public static <T> RestResponse<T> error(Integer code, String msg) {
        return new RestResponse(false, code, msg, null);
    }
}
